package org.nnxy.foodboot.controller;

import org.nnxy.foodboot.common.Result;
import org.nnxy.foodboot.entity.UserRating;
import org.nnxy.foodboot.service.UserRatingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * 用户评分控制器
 */
@RestController
@RequestMapping("/rating")
public class UserRatingController {
    
    @Autowired
    private UserRatingService userRatingService;
    
    /**
     * 添加或更新评分
     * @param userId 用户ID
     * @param recipeId 食谱ID
     * @param rating 评分
     * @param comment 评论内容
     * @return 操作结果
     */
    @PostMapping("/add-or-update")
    public Result<Boolean> addOrUpdateRating(
            @RequestParam Long userId,
            @RequestParam Long recipeId,
            @RequestParam Integer rating,
            @RequestParam(required = false) String comment) {
        
        boolean result = userRatingService.addOrUpdateRating(userId, recipeId, rating, comment);
        return Result.success(result);
    }
    
    /**
     * 删除评分
     * @param userId 用户ID
     * @param recipeId 食谱ID
     * @return 操作结果
     */
    @DeleteMapping("/delete")
    public Result<Boolean> deleteRating(
            @RequestParam Long userId,
            @RequestParam Long recipeId) {
        
        boolean result = userRatingService.deleteRating(userId, recipeId);
        return Result.success(result);
    }
    
    /**
     * 获取食谱平均评分
     * @param recipeId 食谱ID
     * @return 平均评分
     */
    @GetMapping("/average/{recipeId}")
    public Result<BigDecimal> getAverageRating(@PathVariable Long recipeId) {
        BigDecimal avgRating = userRatingService.getAverageRating(recipeId);
        return Result.success(avgRating);
    }
    
    /**
     * 获取用户对食谱的评分
     * @param userId 用户ID
     * @param recipeId 食谱ID
     * @return 用户评分
     */
    @GetMapping("/user-rating")
    public Result<UserRating> getUserRating(
            @RequestParam Long userId,
            @RequestParam Long recipeId) {
        
        UserRating rating = userRatingService.getUserRating(userId, recipeId);
        return Result.success(rating);
    }
    
    /**
     * 获取食谱的所有评价
     * @param recipeId 食谱ID
     * @return 评价列表
     */
    @GetMapping("/recipe-ratings/{recipeId}")
    public Result<List<UserRating>> getRecipeRatings(@PathVariable Long recipeId) {
        List<UserRating> ratings = userRatingService.getRecipeRatings(recipeId);
        return Result.success(ratings);
    }
    
    /**
     * 获取用户的所有评分记录（分页）
     * @param userId 用户ID
     * @param page 页码
     * @param size 每页数量
     * @return 用户评分记录
     */
    @GetMapping("/user-ratings")
    public Map<String, Object> getUserRatings(
            @RequestParam Long userId,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        
        Page<UserRating> pageParam = new Page<>(page, size);
        LambdaQueryWrapper<UserRating> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserRating::getUserId, userId)
                    .eq(UserRating::getDelFlag, "0")
                    .orderByDesc(UserRating::getCreateTime);
        
        Page<UserRating> resultPage = userRatingService.page(pageParam, queryWrapper);
        
        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("data", resultPage);
        return result;
    }
} 